package com.woniu.backstage.controller;


import com.alibaba.fastjson.JSON;
import com.woniu.backstage.common.PageResult;
import com.woniu.backstage.common.Result;
import com.woniu.backstage.entity.TbVip;
import com.woniu.backstage.service.ITbVipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author spirit
 * @since 2020-04-17
 */
@RestController
@RequestMapping("/backstage/vip")
public class TbVipController {
    @Autowired
    private ITbVipService vipService;

    @RequestMapping("/vipShow")
    public String getVip(int page,int limit){
        int pageSize = limit;
        PageResult result = vipService.getAllVip(page, pageSize);
        return JSON.toJSONString(result);
    }

    @RequestMapping("/addVip")
    public Result addVip(TbVip vip) throws Exception {
        int result = vipService.addVip(vip);
        if(result == 1){
            return Result.success();
        }
        throw new Exception("添加失败");
    }

    @RequestMapping("/deleteVip")
    public Result deleteVip(Integer id) throws Exception {
        int result = vipService.deleteVip(id);
        if(result == 1){
            return Result.success();
        }
        throw new Exception("删除失败");
    }

    @RequestMapping("/updateVip")
    public Result updateVip(TbVip vip) throws Exception {
        int result = vipService.updateVip(vip);
        if(result == 1){
            return Result.success();
        }
        throw new Exception("修改失败");
    }

    @RequestMapping("/deleteBatch")
    public Result deleteBatch(Integer[] idArr) throws Exception {
        Integer result = vipService.deleteBatch(Arrays.asList(idArr));
        System.out.println(result);
        if(result > 0){
            return Result.success();
        }
        throw new Exception("删除失败");
    }
}

